<?php 
	session_start();	
	if (!isset($id)) {
		window.location.assign("homeAdmin.php");
	}
	$rows_per_page = 10;
	$page_start = ( $page - 1 ) * $rows_per_page; 
	$page_end = $page * $rows_per_page;
	
	unset($_SESSION['searchstring']);
	$order_by = " order by FullName ASC ";
	$_SESSION['searchstring'] = $order_by;
	
	$listemployeePage = mysql_query("SELECT em.IDEmployee FROM employee em left JOIN department_lang dpl
	 on em.IDDepartment = dpl.IDDepartment where em.State = 1 and dpl.LangID='".$lang."' ".$order_by);
	
	$listemployee = mysql_query("SELECT em.IDEmployee,em.UserName, em.FullName ,dpl.DepartmentName, em.Position ,em.Sex, em.IDDepartment FROM employee em left JOIN department_lang dpl
	 on em.IDDepartment = dpl.IDDepartment where em.State = 1 and dpl.LangID='".$lang."' ".$order_by." limit ".$page_start.",".$rows_per_page);
	
	$number_of_page_employees = ceil ( mysql_num_rows( $listemployeePage ) / $rows_per_page );
	//search
	$listDepartments = mysql_query("select department_lang.IDDepartment,department_lang.DepartmentName,department_lang.LangID 
					from department_lang where department_lang.LangID = '".$lang."'");
	
	$listSex = mysql_query("select SexID,SexName from ac_sex_lang WHERE LangID='".$lang."'");
	
	if (isset($id)) {
		$query_project_ = mysql_query("SELECT p.IDProject, p.NameProject, p.DateStart
	        	FROM pr_project p WHERE p.IDProject='".$id."'  LIMIT 1");
		while ($row_p = mysql_fetch_array($query_project_)) {
			$projectname_ = $row_p[NameProject];
			$startdate_ = $row_p[DateStart];
			
		}
	}
	/*$listcomposition = mysql_query("SELECT c.IDComposition, c.IDEmployee, c.IDProject, c.Position, c.Status, e.UserName, e.FullName, 
        		 e.Position as PositionEmp FROM pr_composition c, employee e 
        		 WHERE c.IDProject = ".$id." AND c.IDEmployee = e.IDEmployee");
	$tester = array();
	$coder = array();
	while ($row = mysql_fetch_array($listcomposition)) {
		if ($row['Status']==0) {
			if ($row['Position']==0) {
				array_push($tester,array($row[IDComposition],$row[IDEmployee],$row[UserName],$row[FullName],$row[Position],$row[Status]));
			}else{
				array_push($coder,array($row[IDComposition],$row[IDEmployee],$row[UserName],$row[FullName],$row[Position],$row[Status]));
			}
		}
		
	}*/
	
?>

<script type="text/javascript">
//alert(testers);

function sortFullname(numberspage){
	var orderby = $('input#sortfullname').val();	
	$.ajax({url:"admin/project/ajaxsortemployee.php", data : {orderby : orderby , flags : 'fullname'}, success:function(result){
		$("table#lstems tr:not(:first)").remove();
		$("table#lstems tbody").append(result);
		if (orderby==<?=SORT_ASC?>){
			$('input#sortfullname').val(<?=SORT_DESC?>);
		}else{
			$('input#sortfullname').val(<?=SORT_ASC?>);
		}
	}});
}

function sortPosition(numberspage){
	var orderby = $('input#sortposition').val();	
	$.ajax({url:"admin/project/ajaxsortemployee.php", data : {orderby : orderby , flags : 'position'}, success:function(result){
		$("table#lstems tr:not(:first)").remove();
		$("table#lstems tbody").append(result);
		if (orderby==<?=SORT_ASC?>){
			$('input#sortposition').val(<?=SORT_DESC?>);
		}else{
			$('input#sortposition').val(<?=SORT_ASC?>);
		}
	}});
}

function paging(page, eleID){
	$.ajax({url:"admin/project/ajaxpagingemployee.php", data : {pages : page}, success:function(result){
		$("table#lstems tr:not(:first)").remove();
		$("table#lstems tbody").append(result);
	}});
}

function searchAll( eleID){
	$('input#searchFN').val('');
	$.ajax({url:"admin/project/ajaxpagingemployee.php", data : {searchAll : 'all'}, success:function(result){
		$("table#lstems tr:not(:first)").remove();
		$("table#lstems tbody").append(result);
	}});
}

function searchEmployee(){	
	var departments = $('select#searchDepartment').val();
	var sexs = $('select#searchSex').val();
	var searchv = $('input#searchFN').val();
	$.ajax({url:"admin/project/ajaxsearchemployee.php", data : {department : departments, sex : sexs, searchv : searchv }, success:function(result){
		$("table#lstems tr:not(:first)").remove();
		$("table#lstems tbody").append(result);
	}});
}

function searchChangeDepartment(){	
	var departments = $('select#searchDepartment').val();
	$.ajax({url:"admin/project/ajaxsearchauto.php", data : { department : departments }, success:function(result){
		$("table#lstems tr:not(:first)").remove();
		$("table#lstems tbody").append(result);
	}});
}

function searchChangeSex(){	
	var sexs = $('select#searchSex').val();
	$.ajax({url:"admin/project/ajaxsearchauto.php", data : { sex : sexs}, success:function(result){
		$("table#lstems tr:not(:first)").remove();
		$("table#lstems tbody").append(result);
	}});
}

function addTester(ele){
	var username = $(ele).parent().find('input').val();
	var fullname = $(ele).parent().parent().find('.FullName').html();	
	var idemployee = $(ele).parent().parent().find('.IDEmployee').find('input').val();
	$.ajax({url:"admin/project/ajaxTester.php", data : {action : 'add', idemployee : idemployee, username : username, fullnames : fullname}, success:function(result){
		if (result) {
			$('table#addtester tr:not(:first)').remove();
			$('table#addtester').append(result);
		}
		
	}});	
}

function removeTester(ele){
	var idemployee = $(ele).parent().parent().find('td:first').find('input').val();
	var trO = $(ele).parent().parent();
	var rowIndex = $(ele).closest("tr").index()-1;
	$.ajax({url:"admin/project/ajaxTester.php", data : {action : 'remove',idemployee : idemployee, rowIndex : rowIndex}, success:function(result){
		$('table#addtester tr:not(:first)').remove();
		$('table#addtester').append(result);
	}});
}

function addCoder(ele){
	var username = $(ele).parent().find('input').val();
	var fullname = $(ele).parent().parent().find('.FullName').html();	
	var idemployee = $(ele).parent().parent().find('.IDEmployee').find('input').val();
	$.ajax({url:"admin/project/ajaxCoder.php", data : {action : 'add', idemployee : idemployee, username : username, fullnames : fullname}, success:function(result){
		if (result) {
			$('table#addcoder tr:not(:first)').remove();
			$('table#addcoder').append(result);
		}
	}});
	/*var username = $(ele).parent().find('input').val();
	var fullname = $(ele).parent().parent().find('.FullName').html();
	var rowCount = $('table#addcoder tr').length;	
	var idemployee = $(ele).parent().parent().find('.IDEmployee').find('input').val();
	if(coders.indexOf(idemployee)==-1){
		coders.push(idemployee);
		$('table#addcoder').append("<tr><td>"+rowCount+"<input type='hidden' value='"+idemployee+"' /></td><td>"+username+"</td><td>"+fullname+"</td><td><span onmouseover=this.style.cursor='pointer' onmouseout=this.style.cursor='default' onclick='removeCoder(this)'><img src='images/del.png'/></span></td></tr>");
	}*/	
}

function removeCoder(ele){
	var idemployee = $(ele).parent().parent().find('td:first').find('input').val();
	var trO = $(ele).parent().parent();
	var rowIndex = $(ele).closest("tr").index()-1;
	$.ajax({url:"admin/project/ajaxCoder.php", data : {action : 'remove',idemployee : idemployee, rowIndex : rowIndex}, success:function(result){
		$('table#addcoder tr:not(:first)').remove();
		$('table#addcoder').append(result);
	}});
	/*var idemployee = $(ele).parent().parent().find('td:first').find('input').val();
	$(ele).parent().parent().remove();
	var rowIndex = coders.indexOf(idemployee);
	if(rowIndex!=-1){
		coders.splice(rowIndex,1);
	}*/
}

function updateMember(projectid){
	$.ajax({url:"admin/project/ajaxUpdateMember.php", data : {projectid : projectid}, success:function(result){
		window.location.assign("project_admin.php?content=admin/project/project-detail.php&id="+projectid);
	}});
}

</script>

<div id="contains" style="background-color: #edf4f7;">

	<div class="title"></div>
	<div class="main_content"  style="min-height: 895px">  
		<div class="tr_title"><?=$l_testerandcoder?> </div>	
            <div style="width: 100%; margin-top:5px">
				<table cellpadding="5" cellspacing="5" style="width: 100%">
	                <tr>
		                <td width="25%"><?php echo $l_project_name.': '.$projectname_;?></td>
		                <td width="25%"><?php echo $l_start_date.': '.$startdate_;?></td>
		                <td width="50%"></td>
		               
	                </tr>
	               
	                <tr>
		                <td class="rf-dt-ftr-c">
		                	<select onchange="searchChangeDepartment()" class="com_tit" style="width: 70%" id="searchDepartment">
		                		<?php while ($rows = mysql_fetch_array($listDepartments)){?>
		                			<option value="<?=$rows[IDDepartment]?>"><?=$rows[DepartmentName]?></option>
		                		<?php }?>
		                	</select>
		                	<select onchange="searchChangeSex()" class="com_tit" id="searchSex">
		                		<?php while ($sexs = mysql_fetch_array($listSex)){?>
		                			<option value="<?=$sexs[SexID]?>"><?=$sexs[SexName]?></option>
		                		<?php }?>
		                	</select>
		                </td>
		                <td class="rf-dt-ftr-c">
		                	<input type="text" id="searchFN" onkeyup="searchEmployee()" style="height: 20px; width: 100%; border: 1px solid #c4c0c9"/>
		                </td>		            
		                <td align="left" style="vertical-align: middle;">
		                	<span class="buttonProject" onmouseover="this.style.cursor='pointer'" onmouseout="this.style.cursor='default'" onclick="searchEmployee()"  style="padding: 5px; border:1px solid #999"><?=$l_search?></span>
		                	|
		                	<span class="buttonProject" onmouseover="this.style.cursor='pointer'" onmouseout="this.style.cursor='default'" onclick="searchAll(1,'')" style="padding: 5px; border:1px solid #999"><?=$l_all?> </span>
		                </td>
	                </tr>
	             </table>
			</div>
            <div id="listEmps">
            	<table cellpadding="5" cellspacing="0" class="rf_dt" id="lstems">
	                <tr  class="table_new">
		                <td width="25%">
		                <a href="#" onclick="sortFullname(<?=$number_of_page_employees?>)"> <?=$l_fullname?></a>
		                <input type="hidden" id="sortfullname" value="<?=SORT_ASC?>"/>
		                </td>
		                <td width="15%"><?=$l_department?></td>
		                <td width="20%">
		                <a href="#" onclick="sortPosition(<?=$number_of_page_employees?>)"><?=$l_position?></a>
		                <input type="hidden" id="sortposition" value="<?=SORT_ASC?>"/>
		                </td>
		                <td width="40%"></td>
	                </tr>
	                <?php 
	                unset($_SESSION['lstemps']);
	                $temp_lstemps = array();
	                while ($row=mysql_fetch_array($listemployee)){ 
	                	array_push($temp_lstemps, array($row[IDEmployee],$row[UserName],$row[FullName],$row[DepartmentName],$row[Position],$row[Sex],$row[IDDepartment]));
	                	
					?>
	                <tr>
		                <td align="center" class="FullName"><?=$row[FullName]?></td>
		                <td align="center"><?=$row[DepartmentName]?></td>
		                <td align="center" class="IDEmployee"><?=$row[Position]?><input type="hidden" value="<?=$row[IDEmployee]?>"/></td>
		                <td align="left">
		                	<span class="buttonProject" onmouseover="this.style.cursor='pointer'" onmouseout="this.style.cursor='default'" onclick="addTester(this)" style="padding: 5px; border:1px solid #999"><?=$_addtester?></span>
		                	|
		                	<span class="buttonProject" onmouseover="this.style.cursor='pointer'" onmouseout="this.style.cursor='default'" onclick="addCoder(this)" style="padding: 5px; border:1px solid #999"><?=$_addcoder?> </span>
		                	<input type="hidden" value="<?=$row[UserName]?>"/>
		                </td>
	                </tr>
	                <?php }
	                $_SESSION['lstemps'] = $temp_lstemps;
	                $temp_lstemps = null;
	                ?>
	                <tr class="trfoot">
	                    	<td class="rf-dt-ftr-c" scope="colgroup" colspan="6">
	                        	<div style="text-align: center; width: 100%">
	                            	<span class="rf-ds">
															
															<?php if ( $number_of_page_employees > 1 ) 
															{ 
																if ($page==1)
																	$p=2;
																else if ($page==$number_of_page_employees)
																	$p=$number_of_page_employees-1;
																else $p=$page;
																if($page!=1){?>
																	<a onclick="paging(1,'#listEmps')" ><span class='button_next'><?php echo "<<<";?></span> </a>
																	<a onclick="paging(<?=($page-1)?>,'#listEmps')" ><span class='button_next'><?php echo "<<";?></span> </a>
																<?php  }
																else{
																	echo "<span class='button_next'><<<</span>"; 
																	echo "<span class='button_next'><<</span>";
																}
																for ( $i = $p-1; $i <= $p+1; $i++ ) {	
																		if ($i<=$number_of_page_employees && $i>0)
																		
																			if ($i==$page){
																				echo "<span class='button_next' style='color: #000'><i>".$i."</i></span>"; 
																			}else{
																			?>
																				<a onclick="paging(<?=$i?>,'#listEmps')" ><span class="button_next">
																				<?php 
																				echo $i;
																				?>
																				</span> </a>
																			<?php 
																			}
																		?>
																	
																<?php 
																		}
																	
																	if($page!=$number_of_page_employees){ ?>
																		<a onclick="paging(<?=($page+1)?>,'#listEmps')"><span class='button_next'><?php echo ">>";?></span> </a>
																		<a onclick="paging(<?=($number_of_page_employees)?>,'#listEmps')" ><span class='button_next'><?php echo ">>>";?></span> </a>
																	<?php }
																	else{
																		echo "<span class='button_next'>>></span> ";
																		echo "<span class='button_next'>>>></span> ";
																	}
																}
															
																
																?>
																													
														</span>
														</div>
														</td>
	                    </tr> 
	            </table>
            </div>
            <div id='testerandcoder'>
	             <?php include 'ajaxAddComposition.php';?>
            </div> 
			<div style="width: 100%">
				<div align="right" style="margin-top: 10px">
					<a href="admin/project/ajaxUpdateMember.php?projectid=<?=$id?>"> <span class="buttonProject" style="padding: 5px; border:1px solid #999"><?=$l_update?></span></a>
				</div>
			</div>    
</div>	
	
	
</div>

